<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="岗位编码" prop="postCode">
				<el-input v-model="queryParams.postCode" placeholder="请输入岗位编码" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="岗位名称" prop="postName">
				<el-input v-model="queryParams.postName" placeholder="请输入岗位名称" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="显示顺序" prop="postSort">
				<el-input v-model="queryParams.postSort" placeholder="请输入显示顺序" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['sxt:gangwei:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['sxt:gangwei:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['sxt:gangwei:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['sxt:gangwei:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="gangweiList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="岗位ID" align="center" prop="postId" />
			<el-table-column label="岗位编码" align="center" prop="postCode" />
			<el-table-column label="岗位名称" align="center" prop="postName" />
			<el-table-column label="显示顺序" align="center" prop="postSort" />
			<!-- <el-table-column label="状态" align="center" prop="status" /> -->
			<el-table-column label="状态" align="center" prop="status">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="图片" align="center" width="100">
			      <template slot-scope="scope">
			        <img :src="scope.row.img" alt="图片" style="width: 50px; height: 35px;">
			      </template>
			    </el-table-column>
			<el-table-column label="备注" align="center" prop="remark" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['sxt:gangwei:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['sxt:gangwei:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改岗位信息对话框 -->
		<el-dialog :title="title" :visible.sync="open" :show-close="false" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="岗位编码" prop="postCode">
					<el-input v-model="form.postCode" placeholder="请输入岗位编码" />
				</el-form-item>
				<el-form-item label="岗位名称" prop="postName">
					<el-input v-model="form.postName" placeholder="请输入岗位名称" />
				</el-form-item>
				<el-form-item label="显示顺序" prop="postSort">
					<el-input-number v-model="form.postSort" controls-position="right" :min="0" />
				</el-form-item>
				<el-form-item label="岗位状态" prop="status">
					<el-radio-group v-model="form.status">
						<el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
							:label="dict.value">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
				
				<el-form-item label="新闻布局" prop="layout">
				  <el-select
				    v-model="form.layout"
				    placeholder="新闻布局"
				    clearable
				    style="width: 240px"
					@change="handleNewsLayoutChange"
				  >
				    <el-option
				      v-for="dict in dict.type.sxt_new_layout"
				      :key="dict.value"
				      :label="dict.label"
				      :value="dict.value"
				    />
				  </el-select>
				</el-form-item>
				
				<el-form-item label="角色" prop="roleId" v-if="value == 3">
				  <el-select v-model="form.roleId" placeholder="请选择角色" clearable style="width: 240px">
				    <el-option
				      v-for="juese in jueseList"
				      :key="juese.roleId"
				      :label="juese.roleName"
				      :value="juese.roleId"
				    />
				  </el-select>
				</el-form-item>

				<!-- <el-input type="file" /> -->



				<div class="upload-box">
					<input type="file" id="fileInput" style="display:none;" @change="handleFileChange" />
					<!-- 自定义上传区域 -->
					<label for="fileInput" class="upload-area" v-if="!imageUrl">
						<span class="add-icon">+</span>
					</label>
					<!-- 图片预览区域 -->
					<div v-if="imageUrl" class="preview-area">
						<img :src="imageUrl" alt="Selected Image Preview" style="max-width: 100%; height: auto;"
							@click="openFileSelector" />
					</div>
				</div>

				<div class="sc_button">
					<el-button class="uploadButton" @click="uploadFile()">上传</el-button>
				</div>
				

				<!-- <div class="no-border-input">
					<input type="file" @change="handleFileChange" />
					<el-button @click="uploadFile()">上传</el-button>
				</div> -->

				<el-form-item label="备注" prop="remark" class="beizhu">
					<el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import request from '@/utils/request'
	import {
		listGangwei,
		getGangwei,
		delGangwei,
		addGangwei,
		updateGangwei
	} from "@/api/sxt/gangwei";
	import {
		listJuese,
		getJuese,
		delJuese,
		addJuese,
		updateJuese
	} from "@/api/sxt/juese";

	export default {
		name: "Gangwei",
		dicts: ['sys_normal_disable','sxt_new_layout'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				selectedFile: 0,
				imageUrl: "",
				value: 0,
				// 岗位信息表格数据
				gangweiList: [],
				jueseList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					postCode: null,
					postName: null,
					postSort: null,
					status: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					postCode: [{
						required: true,
						message: "岗位编码不能为空",
						trigger: "blur"
					}],
					postName: [{
						required: true,
						message: "岗位名称不能为空",
						trigger: "blur"
					}],
					postSort: [{
						required: true,
						message: "显示顺序不能为空",
						trigger: "blur"
					}],
					status: [{
						required: true,
						message: "状态不能为空",
						trigger: "change"
					}],
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询岗位信息列表 */
			//岗位列表
			//岗位列表信息
			getList() {
				this.loading = true;
				listGangwei(this.queryParams).then(response => {
					this.gangweiList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.imageUrl = "";
				this.selectedFile = 0;
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					postId: null,
					postCode: null,
					postName: null,
					postSort: null,
					status: "0",
					roleId: null,
					img: null,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null,
					layout: null,
					remark: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.postId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			handleNewsLayoutChange(value){
				this.value = value;
				console.log(this.value+"==========================")
			},
			/** 新增按钮操作 */
			//添加对话框
			handleAdd() {
				this.reset();
				listJuese().then(reponse => {
					this.jueseList = reponse.rows;
				});
				this.open = true;
				this.title = "添加岗位信息";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const postId = row.postId || this.ids;
				listJuese().then(reponse => {
					this.jueseList = reponse.rows;
				});
				getGangwei(postId).then(response => {
					this.form = response.data;
					this.form.layout = "3";
					this.form.roleId = 4;
					this.imageUrl = response.data.img;
					this.open = true;
					this.title = "修改岗位信息";
				});
			},

			handleFileChange(event) {
				this.selectedFile = event.target.files[0];
				// 创建一个FileReader实例来读取文件
				const reader = new FileReader();
				reader.onload = e => this.imageUrl = e.target.result;
				// 读取文件作为DataURL
				reader.readAsDataURL(this.selectedFile);

			},
			openFileSelector() {
				document.getElementById('fileInput').click();
			},
			showSuccessMessage() {
				this.$modal.msgSuccess("上传成功");
				// 方法1：简单的alert弹窗
				//alert('图片上传成功！');
			},
			uploadFile() {
				//console.log("123654");
				if (!this.selectedFile) {
					alert("请选择文件！");
					return;
				}
				let formData = new FormData();
				formData.append('file', this.selectedFile);

				request.post('/common/upload', formData, {
					headers: {
						'Content-Type': 'multipart/form-data'
					}
				}).then(response => {
					//console.log('文件上传成功:', response.fileName);
					//console.log(response.url)
					this.form.img = response.url;
					this.showSuccessMessage(); // 显示成功消息给用户

				}).catch(error => {
					console.error('文件上传失败:', error);
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.postId != null) {
							updateGangwei(this.form).then(response => {
								console.log(this.form.layout+"-----------------------------");
								console.log(this.form.roleId+"-----------------------------");
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addGangwei(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
							//console.log(this.form.jueseId+"123654789");
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const postIds = row.postId || this.ids;
				this.$modal.confirm('是否确认删除岗位信息编号为"' + postIds + '"的数据项？').then(function() {
					return delGangwei(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('sxt/gangwei/export', {
					...this.queryParams
				}, `gangwei_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
<style lang="scss">
	.no-border-input {
		margin-top: -10px;
	}

	.no-border-input input {
		border: none;
		/* 去掉边框 */
		/* padding: 0; */
		/* 如果需要，可以移除内边距 */
		/* margin: 0; */
		/* 如果需要，可以移除外边距 */
		/* 可以根据需要添加更多样式 */
		margin-left: 15px;
	}

	.beizhu {
		/* margin-top: 20px; */
	}

	/* 自定义文件输入框样式 */
	input[type="file"] {
		display: block;
		margin: 20px 0;
		padding: 10px;
		background-color: #f0f0f0;
		border-radius: 4px;
	}

	/* 图片预览样式 */
	img {
		border: 1px solid #ddd;
		border-radius: 4px;
		padding: 5px;
		box-shadow: 0 0 5px rgba(0, 0, 0, .3);
	}

	.upload-box {
		overflow: hidden;
		position: relative;
		width: 200px;
		height: 200px;
		margin-left: 20px
	}

	.upload-area {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 100%;
		border: 2px dashed #ccc;
		border-radius: 4px;
		cursor: pointer;
		font-size: 30px;
		color: #ccc;
	}

	.add-icon {
		font-size: 3em;
	}

	.preview-area img {
		/* width: 100%;
	  height: 100%; */
		width: 200px;
		height: 200px;
		object-fit: cover;
		border-radius: 4px;
		cursor: pointer;
	}

	.sc_button {
		width: 60px;
		height: 60px;
		margin-left: 80px;
		margin-top: 20px;
	}
	.uploadButton{
		background-color: #009100;
		color: aliceblue;
		font-weight: 700;
		font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
	}
	
	
</style>